package nl.tygron.niftyconverter.elements;

import java.awt.Color;
import java.awt.Font;
import org.w3c.dom.Element;

/**
 * @author Raymond Weijermars
 * @Specialism GUI
 * @Maintainer Jeroen Warmerdam
 */

public class NButtonElement extends NImageElement {

	/**
	 * <image id="continue" filename="/Images/menu_narrow_inverse_out.png" visibleToMouse="true" x="760" y="600"> <effect> <onHover
	 * name="imageOverlay" filename="Interface/Nifty/Images/menu_narrow_inverse_over.png" post="true"/> </effect> <interact onClick="cont()"
	 * /*
	 */

	protected Element elementEffect;
	protected Element elementOnHover;
	protected Element elementInteract;
	protected NTextElement nTextElement;

	/**
	 * Constructor for image buttons without a textlabel.
	 */
	public NButtonElement(String id, int x, int y, int panelWidth, int panelHeight, int iconWidth, int iconHeight,
	        NiftyStructure nStructure, String imageFileName, String align, String valign) {

		super(id, x, y, panelWidth, panelHeight, iconWidth, iconHeight, nStructure, imageFileName, align, valign, true);
		initStandardNButtonElements(id, nStructure);
	}

	/**
	 * Constructor for image buttons with a textlabel.
	 */
	public NButtonElement(String id, int x, int y, int labelWidth, int labelHeight, NiftyStructure nStructure, String imageFileName,
	        String labelText, Font font, Color labelColor, String align, String valign) {

		super(id, x, y, labelWidth, labelHeight, nStructure, imageFileName, labelText, font, labelColor, align, valign, true);
		initStandardNButtonElements(id, nStructure);
	}

	/**
	 * Constructor for image buttons without a textlabel.
	 */
	public NButtonElement(String id, int x, int y, int panelWidth, int panelHeight, NiftyStructure nStructure, String imageFileName,
	        String align, String valign) {

		super(id, x, y, panelWidth, panelHeight, nStructure, imageFileName, align, valign, true);
		initStandardNButtonElements(id, nStructure);
	}

	private void initStandardNButtonElements(String id, NiftyStructure nStructure) {

		this.addAttribute("childClip", "true");
		elementEffect = nStructure.newElement("effect");
		this.childImageElement.getXML().appendChild(elementEffect);
		this.childImageElement.getXML().setAttribute("visibleToMouse", "true");
		elementInteract = nStructure.newElement("interact");
		elementInteract.setAttribute("onRelease", "buttonClicked(" + id + ")");
		this.childImageElement.getXML().appendChild(elementInteract);
	}

	public void setHoverIcon(String imageHoverFileName, NiftyStructure nStructure, String id) {

		elementOnHover = nStructure.newElement("onHover");
		elementOnHover.setAttribute("name", "imageOverlay");
		elementOnHover.setAttribute("filename", imageHoverFileName);
		elementOnHover.setAttribute("post", "true");
		elementOnHover.setAttribute("onStartEffect", "buttonHovered(" + id + ")");
		elementOnHover.setAttribute("onEndEffect", "buttonUnhovered(" + id + ")");
		elementEffect.appendChild(elementOnHover);
	}
}
